# _*_ coding : utf-8 _*_
import win32gui
import win32api
import win32con
from win32con import *
import win32clipboard as w
import time
import datetime
import random
import threading
from concurrent.futures import  ThreadPoolExecutor, as_completed
lock = threading.Lock()

lis = ['#签到', '#打卡','#美滋滋']
def time_count(interval=20):
    # t1 = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    t1 = datetime.datetime.now().strftime("%H:%M:%S")
    print('current time is [{}]'.format(t1))
    # t2 = (datetime.datetime.now()+datetime.timedelta(minutes=5)).strftime("%Y-%m-%d %H:%M:%S")
    t2 = (datetime.datetime.now() +
          datetime.timedelta(minutes=interval)).strftime("%H:%M:%S")
    print('\n[{}] woulde be incoming.'.format(t2))


def getContentFromClipboared_u(content="寿喜锅"):

    # 打开剪贴板
    w.OpenClipboard()
    # 清空剪贴板
    w.EmptyClipboard()
    # 设置剪贴板内容
    # w.SetClipboardData(win32con.CF_UNICODETEXT, "寿喜锅")
    w.SetClipboardData(win32con.CF_UNICODETEXT, content)
    # 获取剪贴板内容
    date = w.GetClipboardData()
    # 关闭剪贴板
    w.CloseClipboard()


def mykey_press_release_u(keyno:int = 1):
    # key 1 : 0x31 or 49
    # key 2 : 0x32 or 50
    # key 3 : 0x33 or 51
    # ctrl  : 0x11 or 17
    # alt   : 0x12 or 18

    # 按下ctrl+v
    # win32api.keybd_event(0x11, 0, 0, 0)
    # win32api.keybd_event(0x56, 0, 0, 0)
    # win32api.keybd_event(0x56, 0, win32con.KEYEVENTF_KEYUP, 0)
    # win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
    # time.sleep(1)
    # # 按下回车
    # win32api.keybd_event(0x0D, 0, 0, 0)
    # win32api.keybd_event(0x0D, 0, win32con.KEYEVENTF_KEYUP, 0)
    if keyno == 1:
        win32api.keybd_event(0x11, 0, 0, 0)
        win32api.keybd_event(0x31, 0, 0, 0)
        win32api.keybd_event(0x31, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
    elif keyno == 2:
        win32api.keybd_event(0x11, 0, 0, 0)
        win32api.keybd_event(0x32, 0, 0, 0)
        win32api.keybd_event(0x32, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
    elif keyno == 3:
        win32api.keybd_event(0x11, 0, 0, 0)
        win32api.keybd_event(0x33, 0, 0, 0)
        win32api.keybd_event(0x33, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
    elif keyno == 4:
        win32api.keybd_event(0x11, 0, 0, 0)
        win32api.keybd_event(0x34, 0, 0, 0)
        win32api.keybd_event(0x34, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
    elif keyno == 5:
        win32api.keybd_event(0x11, 0, 0, 0)
        win32api.keybd_event(0x35, 0, 0, 0)
        win32api.keybd_event(0x35, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
    elif keyno == 6:
        win32api.keybd_event(0x11, 0, 0, 0)
        win32api.keybd_event(0x36, 0, 0, 0)
        win32api.keybd_event(0x36, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
    elif keyno == 7:
        win32api.keybd_event(0x11, 0, 0, 0)
        win32api.keybd_event(0x37, 0, 0, 0)
        win32api.keybd_event(0x37, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)

def findwindowfrommouse_daka30(keyno:int=1, interval:int=30):
    # time.sleep(2)
    mykey_press_release_u(keyno)
    time.sleep(2)
    time_count(interval)
    phw = win32gui.WindowFromPoint(win32api.GetCursorPos())
    # print('phw', phw)
    # print('hex(phw)', hex(phw))
    title = win32gui.GetWindowText(phw)
    # print(title)
    myposition = win32gui.GetWindowRect(phw)
    # print(myposition)

    # getContentFromClipboared()
    # # 打开剪贴板
    # w.OpenClipboard()
    # # 清空剪贴板
    # w.EmptyClipboard()
    # # 设置剪贴板内容
    # w.SetClipboardData(win32con.CF_UNICODETEXT, "寿喜锅")
    # # 获取剪贴板内容
    # date = w.GetClipboardData()
    # # 关闭剪贴板
    # w.CloseClipboard()

    # 显示窗口 (1347, 761) 1394, 764
    win32gui.ShowWindow(phw, win32con.SW_SHOW)
    win32api.SetCursorPos([1347, 761])
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 1394, 764, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 1394, 764, 0, 0)
    time.sleep(2)

    # getContentFromClipboared_u(random.choice(lis1) + ',')
    if keyno != 2:
        getContentFromClipboared_u(lis[1])
    else:
        getContentFromClipboared_u(lis[2])

    # 把剪切板内容粘贴到窗口
    win32gui.SendMessage(phw, win32con.WM_PASTE, 0, 0)
    time.sleep(1)
    # 按下后松开回车键，发送消息
    win32gui.SendMessage(phw, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
    win32gui.SendMessage(phw, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
    time.sleep(1)  # 延缓进程

def run_daka(interval: int = 30):
    for i in range(42):
        findwindowfrommouse_daka30(3, interval)
        findwindowfrommouse_daka30(4, interval)
        findwindowfrommouse_daka30(5, interval)
        findwindowfrommouse_daka30(6, interval)
        time.sleep(60 * interval)

def consumer_noproduct(num:int = 20, iinterval:int = 30, keynum:int = 2):
    for i in range(num):
        # time.sleep(2)
        print("---wait for producer running---")
        print("cser-current thread name is %s" %
              threading.current_thread().name)
        # print("wait-6-seconds---consumer finish---No.:[%s]" % item)
        lock.acquire()
        # fun_daka(1, "" , iinterval, keynum, word)
        findwindowfrommouse_daka30(keynum, iinterval)
        # time.sleep(2)
        lock.release()
        time.sleep(60 * iinterval + 2)
        

def run_daka_lock(interval: int = 30):
    dict1 = {"num": 40, "iinterval":20, "keynum": 1}
    dict2 = {"num": 40, "iinterval":35, "keynum": 2}
    dict3 = {"num": 40, "iinterval":30, "keynum": 3}
    dict4 = {"num": 40, "iinterval":30, "keynum": 4}
    dict5 = {"num": 40, "iinterval":30, "keynum": 5}
    dict6 = {"num": 40, "iinterval":30, "keynum": 6}

    t1 = threading.Thread(target=consumer_noproduct, kwargs=dict1)
    t2 = threading.Thread(target=consumer_noproduct, kwargs=dict2)
    t3 = threading.Thread(target=consumer_noproduct, kwargs=dict3)
    t4 = threading.Thread(target=consumer_noproduct, kwargs=dict4)
    t5 = threading.Thread(target=consumer_noproduct, kwargs=dict5)
    t6 = threading.Thread(target=consumer_noproduct, kwargs=dict6)
    
    t1.start()
    t2.start()
    t3.start()
    t4.start()
    t5.start()
    t6.start()
    
    t1.join()
    t2.join()
    t3.join()
    t4.join()
    t5.join()
    t6.join()
    
def run_daka_lock_pool():
    pool = ThreadPoolExecutor(max_workers=7, thread_name_prefix="thread")
    future1 = pool.submit(consumer_noproduct, 41, 20, 1)
    # time.sleep(4)
    future2 = pool.submit(consumer_noproduct, 41, 35, 2)
    # time.sleep(4)
    future3 = pool.submit(consumer_noproduct, 41, 30, 3)
    # time.sleep(4)
    future4 = pool.submit(consumer_noproduct, 41, 30, 4)
    # time.sleep(4)
    future5 = pool.submit(consumer_noproduct, 41, 30, 5)
    # time.sleep(4)
    future6 = pool.submit(consumer_noproduct, 41, 30, 6)
    # time.sleep(4)
    future7 = pool.submit(consumer_noproduct, 41, 30, 7)

    for future in as_completed([future1, future2, future3, future4, future5, future6, future7]):
        print("future done: ", future.done())
        print("\nresult: ", future.result())

    pool.shutdown()
    

if __name__ == '__main__':
    # findwindowfrommouse_daka30()
    # run_daka(30)
    # run_daka_lock(30)
    run_daka_lock_pool()
    print('Hello Python!')

